package filesystem;


import controller.Compiler;
import filesystem.model.Disk;
import filesystem.model.FAT;
import filesystem.model.File;
import filesystem.service.DiskService;
import filesystem.service.FileService;
import util.ExecutionFileGenerator;

import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Random;

public class GlobalFSData {
	private static Disk disk;
	private static List<Object> currentFiles;
	private static List<Object> currentDirs;
	private static List<Object> currentFilesAndDirs;
	public static FAT fat;

	public static List<File> exeFiles = new ArrayList<>();

	private static List<File> currentOpenFile = new ArrayList<>();

	public static File getRoot() {
		return (File) DiskService.getDiskContent(2, GlobalFSData.getDisk());
	}

	public static Disk getDisk() {
		return disk;
	}

	public static void setDisk(Disk disk) {
		GlobalFSData.disk = disk;
		setFat(disk.getFat());
	}

	public static List<Object> getCurrentFiles() {
		return currentFiles;
	}

	public static void setCurrentFiles(List<Object> currentFiles) {
		GlobalFSData.currentFiles = currentFiles;
	}

	public static List<Object> getCurrentDirs() {
		return currentDirs;
	}

	public static void setCurrentDirs(List<Object> currentDirs) {
		GlobalFSData.currentDirs = currentDirs;
	}

	public static FAT getFat() {
		return fat;
	}

	public static void setFat(FAT fat) {
		GlobalFSData.fat = fat;
	}

	public static List<Object> getCurrentFilesAndDirs() {
		return currentFilesAndDirs;
	}

	public static void setCurrentFilesAndDirs(List<Object> currentFilesAndDirs) {
		GlobalFSData.currentFilesAndDirs = currentFilesAndDirs;
	}

	public static List<File> getCurrentOpenFile() {
		return currentOpenFile;
	}

	public static void setCurrentOpenFile(List<File> currentOpenFile) {
		GlobalFSData.currentOpenFile = currentOpenFile;
	}

	@SuppressWarnings("unchecked")
	public static void init() {
		Disk d = (Disk) DiskService.achieve2Disk(main.Main.DISK);
		d = DiskService.checkDisk(d);
		GlobalFSData.disk = d;
		GlobalFSData.fat = d.getFat();
		HashMap<String, Object> hash = DiskService.getDirsAndFiles(disk);
		GlobalFSData.setCurrentFiles((List<Object>)hash.get("files"));
		GlobalFSData.setCurrentDirs((List<Object>)hash.get("dirs"));
		GlobalFSData.setCurrentFilesAndDirs((List<Object>)hash.get("allFiles"));

		try {
			GlobalFSData.exeFiles.add(FileService.getFileTraversal("可执行/1.e"));
			GlobalFSData.exeFiles.add(FileService.getFileTraversal("可执行/2.e"));
			GlobalFSData.exeFiles.add(FileService.getFileTraversal("可执行/3.e"));
			GlobalFSData.exeFiles.add(FileService.getFileTraversal("可执行/4.e"));
			GlobalFSData.exeFiles.add(FileService.getFileTraversal("可执行/5.e"));
			GlobalFSData.exeFiles.add(FileService.getFileTraversal("可执行/6.e"));
			GlobalFSData.exeFiles.add(FileService.getFileTraversal("可执行/7.e"));
			GlobalFSData.exeFiles.add(FileService.getFileTraversal("可执行/8.e"));
		} catch (IOException e) {
			e.printStackTrace();
		}
	}

	@SuppressWarnings("unchecked")
	public static void format() {
		Disk d = new Disk();
		GlobalFSData.disk = d;
		GlobalFSData.fat = d.getFat();
		HashMap<String, Object> hash = DiskService.getDirsAndFiles(disk);
		GlobalFSData.setCurrentFiles((List<Object>)hash.get("files"));
		GlobalFSData.setCurrentDirs((List<Object>)hash.get("dirs"));
		GlobalFSData.setCurrentFilesAndDirs((List<Object>)hash.get("allFiles"));
		GlobalFSData.exeFiles = new ArrayList<>();
		GlobalFSData.generateExecutionFile();
	}

	public static void generateExecutionFile() {
		try {
			FileService.createDirectory(GlobalFSData.getRoot(), "可执行");
			File exe = FileService.getFileTraversal("可执行");


			FileService.createFile(exe, "1.e");
			File c1 = FileService.getFileTraversal(exe, "1.e");
			FileService.editFileContent(c1, new String(ExecutionFileGenerator.generateInstructions()));
			c1.setReadOnly(true);
			GlobalFSData.exeFiles.add(c1);

			FileService.createFile(exe, "2.e");
			File c2 = FileService.getFileTraversal(exe, "2.e");
			FileService.editFileContent(c2, new String(ExecutionFileGenerator.generateInstructions()));
			c2.setReadOnly(true);
			GlobalFSData.exeFiles.add(c2);

			FileService.createFile(exe, "3.e");
			File c3 = FileService.getFileTraversal(exe, "3.e");
			FileService.editFileContent(c3, new String(ExecutionFileGenerator.generateInstructions()));
			c3.setReadOnly(true);
			GlobalFSData.exeFiles.add(c3);

			FileService.createFile(exe, "4.e");
			File c4 = FileService.getFileTraversal(exe, "4.e");
			FileService.editFileContent(c4, new String(ExecutionFileGenerator.generateInstructions()));
			c4.setReadOnly(true);
			GlobalFSData.exeFiles.add(c4);

			FileService.createFile(exe, "5.e");
			File c5 = FileService.getFileTraversal(exe, "5.e");
			FileService.editFileContent(c5, new String(ExecutionFileGenerator.generateInstructions()));
			c5.setReadOnly(true);
			GlobalFSData.exeFiles.add(c5);

			FileService.createFile(exe, "6.e");
			File d1 = FileService.getFileTraversal(exe, "6.e");
			FileService.editFileContent(d1, new String(ExecutionFileGenerator.generateInstructions()));
			d1.setReadOnly(true);
			GlobalFSData.exeFiles.add(d1);

			FileService.createFile(exe, "7.e");
			File d2 = FileService.getFileTraversal(exe, "7.e");
			FileService.editFileContent(d2, new String(ExecutionFileGenerator.generateInstructions()));
			d2.setReadOnly(true);
			GlobalFSData.exeFiles.add(d2);

			FileService.createFile(exe, "8.e");
			File d3 = FileService.getFileTraversal(exe, "8.e");
			FileService.editFileContent(d3, new String(ExecutionFileGenerator.generateInstructions()));
			d3.setReadOnly(true);
			GlobalFSData.exeFiles.add(d3);
		} catch (IOException e) {
			e.printStackTrace();
		}
	}

	public static String getExeFiles() {
		try {
			Random r = new Random();
			return Compiler.decompile(FileService.getFileContent(GlobalFSData.exeFiles.get(r.nextInt(GlobalFSData.exeFiles.size()))).getBytes());
		} catch (IOException e) {
			e.printStackTrace();
			return ExecutionFileGenerator.generateInstructions();
		}
	}
}
